﻿using BLL;
using CommonAliPay;
using models;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
namespace yuejilalala
{
    public partial class ShopPay : System.Web.UI.Page
    {
         PayManager marager = new PayManager();
        AddressManager addressmarager = new AddressManager();
        public Pay PaySum = new Pay();
        CartMarager cartMarager = new CartMarager();


        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            { //设置初始价格为0
                PaySum.ProPrice = 0;
                //用户名显示
                userDl();
                if (Session["ddh"]==null&&Session["sessid"]==null)
                {
                    //查询界面 原需要跳转到首页
                    Response.Redirect("Login.aspx");
                }
                bool shdz;
                if (Request.QueryString["shdz"] != null)
                {
                    shdz = bool.Parse(Request.QueryString["shdz"]);
                }
                else
                    shdz = false;
                ///省
                DDL_sheng.DataSource = lianji.lj_shen();
                DDL_sheng.DataTextField = "codename";
                DDL_sheng.DataValueField = "code";
                DDL_sheng.DataBind();
                Response.Write("<script>alser('" + Session["ddh"] + "')</script>");
                if (shdz)
                {
                    HiddenField1.Value = Request.QueryString["ddh"];
                    Session["ddh"]= Request.QueryString["ddh"];
                }
                else
                {
                    HiddenField1.Value = Session["ddh"].ToString();//订单号
                }
                //购买详情初始显示数据(显示出未付款 且订单号正确的)
              
                HiddenField2.Value = Session["sessid"].ToString();//用户id
                string ddh = Session["ddh"].ToString();//接受订单号
                string id = Session["sessid"].ToString();//接受用户ID
                Repeater1.DataSource = marager.SelPay(id, ddh); //显示出订单
                Repeater1.DataBind();
               PaySum = marager.SumCart(HiddenField2.Value, HiddenField1.Value);//显示出总价
               Session["PaySum"] = PaySum;//获取价格
                //加载出当前用户默认收获地址的信息
               List<Address> Listad = addressmarager.get(int.Parse(id));
                //循环集合 当是默认地址时显示出地址
                foreach (Address item in Listad)
                {
                    if (item.Rule==1)
                    {
                        Session["dizhi"] = item;//收货地址
                       //获取省的位置 
                         int she=  item.Addres.IndexOf("省");
                        //省和市中间是市
                        //获取市的位置
                        int shi = item.Addres.IndexOf("市");
                        //市和区中间是区
                        //获取区的位置
                        int qu = item.Addres.IndexOf("区");
                        if (qu==-1)
                        {
                            qu = item.Addres.IndexOf("县");
                        }
                        lb_sheng.Text = item.Addres.Substring(0, she+1);
                        lb_shi.Text = item.Addres.Substring(she+1, shi-she );
                        lb_qu.Text = item.Addres.Substring(shi+1,qu-shi);
                        lb_dz.Text= item.Addres.Substring(qu+1);
                        lb_shr.Text = item.Name;
                        lb_dh.Text = item.Phone.ToString();
                    }
                }
                //收获地址加载
                Repeater2.DataSource = addressmarager.get(int.Parse(id));
                Repeater2.DataBind();
                //判定用户是否登陆 登陆显示购物车内容总数
                if (Session["sessid"] != null)
                {

                    J_MiniCartNum.Text = cartMarager.CartCount((Session["sessid"].ToString())).ProPrice.ToString();//显示出购物车内物品.数量
                }
                orderForm.alipayTradeQuery(Session["ddh"].ToString());//订单号查询
                if (orderForm.ifOrderStatus()=="该订单已经支付")
                {
                    Response.Redirect("information.aspx");
                }
               
            }
  
        }
        public void jz() 
        {
            //购物信息详情显示
            Repeater1.DataSource = marager.SelPay(HiddenField2.Value, HiddenField1.Value); ;
            Repeater1.DataBind();
            if (marager.SumCart(HiddenField2.Value, HiddenField1.Value) != null)
            {
                PaySum = marager.SumCart(HiddenField2.Value, HiddenField1.Value);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //新增用户的收获地址
            Address adds = new Address();
            adds.Name = username.Text;
            adds.Phone = decimal.Parse(userphone.Text);
            adds.Addres = DDL_sheng.Text + DDL_shi.Text + DDL_qu.Text + tb_xq.Text;
            adds.Rule = 1;
            adds.Userid =int.Parse(HiddenField2.Value);
            if (addressmarager.add(adds))
            {
              
                this.Repeater2.DataSource = addressmarager.get(int.Parse(HiddenField2.Value));
                this.DataBind();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("pagepay1.aspx");
        }
        protected void DDL_sheng_SelectedIndexChanged(object sender, EventArgs e)
        {  

        }
        protected void DDL_shi_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取省的下拉框内容
            string selshen = DDL_sheng.SelectedValue;
            DDL_shi.DataSource = lianji.lj_shi(selshen);
            DDL_shi.DataTextField = "codename";
            DDL_shi.DataValueField = "code";
            DDL_shi.DataBind();
           
        }

        protected void DDL_qu_SelectedIndexChanged(object sender, EventArgs e)
        {
            //随市改变而改变
            //获取市的下拉框内容
            string selshen = DDL_shi.SelectedValue;
            DDL_qu.DataSource = lianji.lj_shi(selshen);
            DDL_qu.DataTextField = "codename";
            DDL_qu.DataValueField = "code";
            DDL_qu.DataBind();
        }
        //用户登陆显示
        public void userDl() 
        {
            if (Session["sess"] != null)
            {
                //用户存在则显示出来 并且隐藏注册和登陆显示
                User user = Session["sess"] as User;
                hl_dlm.Text ="用户名:"+ user.UName;
                hl_dlm.Visible = true;//显示登陆名控件
                hl_dl.Visible = false;//请登陆隐藏
                hl_zc.Visible = false;//注册隐藏
            }
            
        }


    }
}